package com.devops.admin.graphql;

import com.devops.admin.model.*;
import com.devops.admin.po.*;
import com.devops.admin.service.*;
import com.devops.admin.vo.DOSPageVo;
import com.devops.admin.vo.DOSProjectVo;
import com.devops.admin.vo.DOSUpdateProjectVo;
import com.devops.common.annotation.DOSLog;
import com.devops.constant.DevOpsConstant;
import com.netflix.graphql.dgs.DgsComponent;
import com.netflix.graphql.dgs.DgsMutation;
import com.netflix.graphql.dgs.DgsQuery;
import com.netflix.graphql.dgs.InputArgument;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @ClassName DOSProjectDataFetcher
 * @Description TODO
 * @Author heyabo
 * @Date 2022/8/4 10:06
 * @Version 1.0
 **/
@DgsComponent
public class DOSProjectDataFetcher {

    @Autowired
    private DOSProjectService dosProjectService;
    @Autowired
    private DOSProjectUserRelationService dosProjectUserRelationService;
    @Autowired
    private DOSProjectModuleService dosProjectModuleService;
    @Autowired
    private DOSProjectEnvClusterNamespaceRelationService dosProjectEnvClusterNamespaceRelationService;


    /**
     * @Author heyabo
     * @Description 查询项目列表
     * @Date 15:35 2022/3/22
     * @Param [projectVo]
     * @return com.devops.admin.model.TableResult<com.devops.admin.po.DOSProjectPo>
     **/
    @DgsQuery
    @DOSLog(description = "查询项目列表")
    @RequiresPermissions("loginUser")
    public TableResult<DOSProjectPo> getProjectList(@InputArgument DOSProjectVo projectVo, @InputArgument DOSPageVo pageVo){

        return dosProjectService.getProjects(projectVo,pageVo);
    }
    /**
     * @return java.lang.Boolean
     * @Author heyabo
     * @Description 添加项目
     * @Date 14:08 2022/3/22
     * @Param [project]
     **/
    @DgsMutation
    @DOSLog(description = "添加项目")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR,DevOpsConstant.RoleCode.ADMINISTRATOR,
            DevOpsConstant.RoleCode.QA_ADMINISTRATOR,DevOpsConstant.RoleCode.TECHNOLOGY_MANAGER},logical = Logical.OR)
    public Boolean saveProject(@InputArgument DOSProject project) {
        return dosProjectService.addProject(project);
    }

    /**
     * @return java.lang.Boolean
     * @Author heyabo
     * @Description 更新项目
     * @Date 14:25 2022/3/22
     * @Param []
     **/
    @DgsMutation
    @DOSLog(description = "更新项目")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR,DevOpsConstant.RoleCode.ADMINISTRATOR,
            DevOpsConstant.RoleCode.QA_ADMINISTRATOR,DevOpsConstant.RoleCode.TECHNOLOGY_MANAGER},logical = Logical.OR)
    public Boolean updateProjects(@InputArgument DOSUpdateProjectVo project) {
        return dosProjectService.updateProject(project);
    }

    /**
     * @return java.lang.Boolean
     * @Author heyabo
     * @Description 根据项目的id删除项目
     * @Date 14:26 2022/3/22
     * @Param [projectId]
     **/
    @DgsMutation
    @DOSLog(description = "根据项目的id删除项目")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR,DevOpsConstant.RoleCode.ADMINISTRATOR,
            DevOpsConstant.RoleCode.QA_ADMINISTRATOR,DevOpsConstant.RoleCode.TECHNOLOGY_MANAGER},logical = Logical.OR)
    public Boolean deleteProject(@InputArgument Integer projectId) {
        return dosProjectService.deleteProjectById(projectId);
    }

    /**
     * @Author heyabo
     * @Description 对项目进行授权
     * @Date 16:52 2022/3/22
     * @Param [authorize]
     * @return java.lang.Boolean
     **/
    @DgsMutation
    @DOSLog(description = "对项目进行授权")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR,DevOpsConstant.RoleCode.ADMINISTRATOR,
            DevOpsConstant.RoleCode.QA_ADMINISTRATOR},logical = Logical.OR)
    public Boolean authorizeProject(@InputArgument List<DOSProjectEnvClusterNamespaceRelation> authorizes){

        return dosProjectEnvClusterNamespaceRelationService.authorizeProject(authorizes);
    }

    /**
     * @Author heyabo
     * @Description 获取项目开发人员信息
     * @Date 11:25 2022/3/23
     * @Param []
     * @return java.util.List<com.devops.admin.model.BaseUser>
     **/
    @DgsQuery
    @DOSLog(description = "获取项目开发人员信息")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR,DevOpsConstant.RoleCode.ADMINISTRATOR,
            DevOpsConstant.RoleCode.QA_ADMINISTRATOR,DevOpsConstant.RoleCode.TECHNOLOGY_MANAGER},logical = Logical.OR)
    public List<DOSProjectUserRelationPo> getProjectDevelopers(@InputArgument Integer projectId){
        return dosProjectUserRelationService.getProjectUsers(projectId,DevOpsConstant.UserType.DEVELOPER);
    }

    /**
     *
     * @param projectId
     * @return
     */
    @DgsQuery
    @DOSLog(description = "获取项目测试人员信息")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR,DevOpsConstant.RoleCode.ADMINISTRATOR,
            DevOpsConstant.RoleCode.QA_ADMINISTRATOR,DevOpsConstant.RoleCode.TECHNOLOGY_MANAGER},logical = Logical.OR)
    public List<DOSProjectUserRelationPo> getProjectQas(@InputArgument Integer projectId){
        return dosProjectUserRelationService.getProjectUsers(projectId,DevOpsConstant.UserType.QA);
    }
    /**
     * @Author heyabo
     * @Description 为项目分配人员
     * @Date 11:38 2022/3/23
     * @Param []
     * @return java.lang.Boolean
     **/
    @DgsMutation
    @DOSLog(description = "为项目分配开发人员")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR,DevOpsConstant.RoleCode.ADMINISTRATOR,
            DevOpsConstant.RoleCode.QA_ADMINISTRATOR,DevOpsConstant.RoleCode.TECHNOLOGY_MANAGER},logical = Logical.OR)
    public Boolean distributionProjectDevelopers(@InputArgument List<DOSProjectUserRelation> relation,@InputArgument Integer projectId){
        dosProjectUserRelationService.distributionUser(relation,projectId,DevOpsConstant.UserType.DEVELOPER);
        return true;
    }

    /**
     * 为项目分配测试人员
     * @param relation
     * @param projectId
     * @return
     */
    @DgsMutation
    @DOSLog(description = "为项目分配测试人员")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR,DevOpsConstant.RoleCode.ADMINISTRATOR,
            DevOpsConstant.RoleCode.QA_ADMINISTRATOR,DevOpsConstant.RoleCode.TECHNOLOGY_MANAGER},logical = Logical.OR)
    public Boolean distributionProjectQas(@InputArgument List<DOSProjectUserRelation> relation,@InputArgument Integer projectId){
        dosProjectUserRelationService.distributionUser(relation,projectId,DevOpsConstant.UserType.QA);
        return true;
    }

    /**
     * @Author heyabo
     * @Description 根据relationId删除项目关联人员
     * @Date 11:24 2022/3/23
     * @Param [userId]
     * @return java.lang.Boolean
     **/
    @DgsMutation
    @DOSLog(description = "删除项目与人员关联关系")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR,DevOpsConstant.RoleCode.ADMINISTRATOR,
            DevOpsConstant.RoleCode.QA_ADMINISTRATOR,DevOpsConstant.RoleCode.TECHNOLOGY_MANAGER},logical = Logical.OR)
    public Integer deleteProjectUserRelation(@InputArgument Integer relationId){
        return dosProjectUserRelationService.removeProjectUserRelation(relationId);
    }

    /**
     * @Author heyabo
     * @Description 添加module
     * @Date 15:45 2022/3/28
     * @Param [module]
     * @return com.devops.common.utils.Message
     **/
    @DgsMutation
    @DOSLog(description = "添加module")
    @RequiresPermissions(DevOpsConstant.RoleCode.LOGIN_USER)
    public DOSProjectModule saveProjectModule(@InputArgument DOSProjectModule module){
        return dosProjectModuleService.saveProjectModule(module);
    }

    /**
     * @Author heyabo
     * @Description 更新module
     * @Date 15:54 2022/3/28
     * @Param [module]
     * @return com.devops.common.utils.Message
     **/
    @DgsMutation
    @DOSLog(description = "更新module")
    @RequiresPermissions(DevOpsConstant.RoleCode.LOGIN_USER)
    public Boolean updateModule(@InputArgument DOSProjectModule module){
        return dosProjectModuleService.updateProjectModule(module);
    }

    /**
     * @Author heyabo
     * @Description 删除module
     * @Date 15:56 2022/3/28
     * @Param [moduleId]
     * @return com.devops.common.utils.Message
     **/
    @DgsMutation
    @DOSLog(description = "删除module")
    @RequiresPermissions(DevOpsConstant.RoleCode.LOGIN_USER)
    public Boolean deleteModule(@InputArgument Integer moduleId){
        return dosProjectModuleService.deleteProjectModule(moduleId);
    }


    /**
     * @Author heyabo
     * @Description 获取权限范围内的项目组
     * @Date 11:12 2022/3/28
     * @Param []
     * @return com.devops.admin.model.DOSEnvAndGroup
     **/
    @DgsQuery
    @DOSLog(description = "获取权限范围内的项目组")
    @RequiresPermissions(value = {DevOpsConstant.RoleCode.SUPER_ADMINISTRATOR,DevOpsConstant.RoleCode.ADMINISTRATOR,
            DevOpsConstant.RoleCode.QA_ADMINISTRATOR,DevOpsConstant.RoleCode.TECHNOLOGY_MANAGER},logical = Logical.OR)
    public DOSEnvAndGroup getEnvAndGroup(){

        return dosProjectService.getEnvAndGroup();
    }


    /**
     * @Author heyabo
     * @Description 根据project获取module
     * @Date 15:35 2022/3/28
     * @Param []
     * @return com.devops.common.utils.Message
     **/
    @DgsQuery
    @DOSLog(description = "根据project获取module")
    @RequiresPermissions(DevOpsConstant.RoleCode.LOGIN_USER)
    public List<DOSProjectModule> getModuleByList(@InputArgument Integer projectId){
        return dosProjectModuleService.getModuleByProjectId(projectId);
    }

    /**
     * @Author heyabo
     * @Description 获取项目详细信息
     * @Date 16:35 2022/6/9
     * @Param []
     * @return com.devops.admin.po.DOSProjectInfoPo
     **/
    @DgsQuery
    @DOSLog(description = "获取项目详细信息")
    @RequiresPermissions(DevOpsConstant.RoleCode.LOGIN_USER)
    public DOSProjectInfoPo getProjectInfo(@InputArgument Integer projectId){

        return dosProjectService.getProjectInfo(projectId);
    }

    /**
     * @Author heyabo
     * @Description 查询绑定和未绑定的资源
     * @Date 08:52 2022/6/10
     * @Param []
     * @return com.devops.admin.po.DOSBindStatusProject
     **/
    @DgsQuery
    @DOSLog(description = "查询绑定和未绑定的资源")
    @RequiresPermissions(DevOpsConstant.RoleCode.LOGIN_USER)
    public List<DOSBindStatusProject> getBindProjects(@InputArgument Integer resourceId){

        return dosProjectService.getBindProjects(resourceId);
    }
}
